package jalgebrava.group.util;

import static jalgebrava.util.collections.IterableMixins.*;
import jalgebrava.group.Group;
import jalgebrava.group.GroupElement;
import jalgebrava.util.F;
import jalgebrava.util.collections.ImmutableIterable;

import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public final class GroupMixins {
	public final static <G> ImmutableIterable<G> elementsOfOrder(final Group<G> group, final int o) {
		return filter(group, new F<G,Boolean>() {

			@Override
			public Boolean apply(G a) {
				return group.order(a) == o;
			}
			
		});
		
	}
	public final static <G> ImmutableIterable<GroupElement<G>> elementsInDescendingOrder(Group<G> g) {
		List<GroupElement<G>> elements = makeList(g.elements());
		Comparator<GroupElement<G>> comparator = new Comparator<GroupElement<G>>() {

			@Override
			public int compare(GroupElement<G> g1, GroupElement<G> g2) {
				return g2.order() - g1.order();
				// is positive when o(g2) > o(g1) 
			}
			
		};
		Collections.sort(elements, comparator);
		return asImmutable(elements);
	}

}
